import org.dom4j.*;

import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ml
 * @create 2017-07-28--13:51
 */
public class Cas {

    private String basePath;
    private String pfuserName;
    private String estateName;
    private String dataBaseIp;
    private String serverIp;
    public Cas(String basePath, String pfuserName, String estateName, String dataBaseIp, String serverIp){
        this.basePath= basePath;
        this.pfuserName=pfuserName;
        this.estateName=estateName;
        this.dataBaseIp=dataBaseIp;
        this.serverIp=serverIp;



    }

    public void changexml() throws Exception{
        Document doc=XmlUtil.parse(basePath+FilePathEnum.CAS_DEPLOYERCONFIGCONTEXT.getValue());
        updateDocByXPATH(doc);

    }

    public void updateDocByXPATH(Document doc) throws DocumentException, IOException {
       /* Element roots = doc.getRootElement();
        System.out.println("根节点 = [" + roots.getName() + "]");
        System.out.println("内容：" + roots.getText());
            //只有根节点……
            Iterator elements=roots.elementIterator();
            while (elements.hasNext()){
                Element child= (Element) elements.next();
                System.out.println("节点名称 = [" + child.getName() + "]"+"节点内容："+child.getText());
                List subElemets=child.elements();
               for(int i=0;i<subElemets.size();i++){
                   Element subChild= (Element) subElemets.get(i);
                   System.out.println("子节点名称："+subChild.getName()+"子节点内容："+subChild.getText());
                   List subsub=subChild.elements();
                   for(int j=0;j<subsub.size();j++){
                       Element element= (Element) subsub.get(j);
                       System.out.println("子子节点名称："+element.getName()+"子子节点内容："+element.getText());
                   }

               }

            }*/



        /*String documentStr=doc.asXML();
        System.out.println(documentStr);

        Pattern pattern=Pattern.compile("\\d{2,3}\\.\\d{2,3}\\.\\d{2,3}\\.\\d{2,3}");
        Matcher matcher=pattern.matcher(documentStr);
        String replaceStr=null;
        while (matcher.find()){
            System.out.println(matcher.group());
             replaceStr=matcher.replaceAll(dataBaseIp);
        }
        Pattern pattern2=Pattern.compile("pfuser");
        Matcher matcher2=pattern2.matcher(replaceStr);
        while (matcher2.find()){
            System.out.println(matcher2.group());
            replaceStr=matcher2.replaceAll(pfuserName);
        }

        doc= DocumentHelper.parseText(replaceStr);
        XmlUtil.saveDocument(doc,"F://1.xml");*/

        Element rootElm = doc.getRootElement();

        System.out.println(rootElm.getName());

        HashMap map=new HashMap();
        map.put("design",rootElm.getNamespaceURI());


        XPath xPath=doc.createXPath("//design:bean[@id='dataSource']/design:property[@name='url']/design:value");

        xPath.setNamespaceURIs(map);

        Element element = (Element) xPath.selectSingleNode(doc);

        System.out.println(element.getText());
        if(element!=null){
            element.setText("jdbc:oracle:thin:@"+dataBaseIp+":1521:orcl");
        }

        XPath xPath2=doc.createXPath("//design:bean[@id='dataSource']/design:property[@name='username']/design:value");
        xPath2.setNamespaceURIs(map);

        Element element2 = (Element) xPath2.selectSingleNode(doc);

        System.out.println(element2.getText());
        if(element2!=null){
            element2.setText(pfuserName);
        }

        XmlUtil.saveDocument(doc,basePath+FilePathEnum.CAS_DEPLOYERCONFIGCONTEXT.getValue());
       /* Node node=doc.selectSingleNode("/beans");
        Map nsMap=new HashMap();
        nsMap.put("beans","http://www.springframework.org/schema/beans");
        //对document而言全路径为：/beans:beans/beans:bean
        XPath xpath=doc.createXPath("beans:bean[@id='dataSource']");

        xpath.setNamespaceURIs(nsMap);
        List<Element> list=xpath.selectNodes(node);
        System.out.println("找到<bean></bean>节点数:"+list.size());
        //打印找到的每个目标节点的内容
        for(Element element:list){
            System.out.println(element.attributeValue("id")+":");
            System.out.println(element.asXML());
        }*/







    }

}
